package cn.dingdan.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dingdan.pojo.Customer;
import cn.dingdan.pojo.Dishes;
import cn.dingdan.sercice.CustomerService;
import cn.dingdan.sercice.DishesService;

@Controller
public class CustomerController {
	
	@Resource(name="customerServiceImpl")
	private CustomerService customerService;
	
	@Resource(name="dishesServiceImpl")
	private DishesService dishesService;
	
	@RequestMapping(value="main",method=RequestMethod.POST)
	@ResponseBody
	public Object main(Integer custId,HttpSession session){
		Customer cu = new Customer();
		cu.setCustId(custId);
		Map<String,Object> map = new HashMap<String, Object>();
		Customer customer =customerService.getCustomer(cu);
		map.put("success",customer!=null);
		if (customer!=null) {
			session.setAttribute("customer", customer);
			 map.put("url", "/main");
		}
		return map;
	}
	/*
	 * 保存客户点餐信息
	 */
	@RequestMapping("/setDishesList")
	public @ResponseBody String  setDishesList(@RequestParam("array[]") Integer array[],
			HttpSession session){
		List<Integer> listArr=Arrays.asList(array);
		List<Dishes> list=dishesService.getEachDishes(listArr);
		if(list.size()<30&list.size()>0){
			Customer customer=(Customer) session.getAttribute("customer");
			if(customer.getList()!=null){
				customer.getList().addAll(list);
			}else{
				customer.setList(list);	
			}
			return "suress";
		}
		return "notAdddown";
	}
	
	/*
	 * 处理获取对象保存的值
	 */
	@RequestMapping("/getDishesList")
	public @ResponseBody List<Dishes> getDishesList(HttpSession session){
		Customer customer=(Customer) session.getAttribute("customer");
		List<Dishes> list=customer.getList();
		for (Dishes dishes : list) {
			System.out.println(dishes.getMenuId());
		}
		return list;
	}
	
	/*
	 * 
	 */
	@RequestMapping("/loginOut")
	public @ResponseBody String loginOut(HttpSession session){
		Customer customer=(Customer) session.getAttribute("Customer");
		if(customer!=null){
			List<Dishes> list=customer.getList();
			if(null == list || list.size()==0){
				return "notlogin";
				}
			}
		session.removeAttribute("customer");
		return "loginOut"; 
	}
}

